package com.weblearning.tliasweb.service.impl;

import com.weblearning.tliasweb.anno.Log;
import com.weblearning.tliasweb.mapper.DeptMapper;
import com.weblearning.tliasweb.mapper.EmpMapper;
import com.weblearning.tliasweb.pojo.Dept;
import com.weblearning.tliasweb.pojo.DeptLog;
import com.weblearning.tliasweb.service.DeptLogService;
import com.weblearning.tliasweb.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    @Override
    public List<Dept> list() {
        return  deptMapper.list();
    }

    @Log
    @Transactional  //加入业务管理
    @Override
    public void delete(Integer id) {
        try {
            deptMapper.deleteById(id);
            System.out.println("输出删除"+id+"部门");
            int i=1/0;

            empMapper.deleteByDeptId(id);

        } finally {
            DeptLog des=new DeptLog();
            des.setCreateTime(LocalDate.now());
            des.setDescription("执行解散部门，解散部门id为"+id+"部门");
            deptLogService.insert(des);
        }

    }

    @Log
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDate.now());
        dept.setUpdateTime(LocalDate.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept select(Integer id) {
        return deptMapper.selectById(id);
    }

    @Log
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDate.now());
        deptMapper.updateById(dept);
    }


}
